﻿using System;
using System.Text;
using System.Windows.Forms;

namespace CodeCrawler.UI.Tools.ViewState {
    
    /// <summary>
    /// ASP.NET ViewState Decoder
    /// </summary>
    internal class Decoder {

        /// <summary>
        /// Decode ViewState
        /// </summary>
        /// <param name="encodedViewState"></param>
        /// <returns></returns>
        internal string DecodeViewState(string encodedViewState) {

            var decodedViewState = string.Empty;
            try {

                byte[] viewStateinBytes = Convert.FromBase64String(encodedViewState);
                decodedViewState        = Encoding.ASCII.GetString(viewStateinBytes);
            }
            catch (FormatException) {

                MessageBox.Show("Invalid ViewState (may not be base64 encoded)", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
            
            }
            return decodedViewState;
        }
    }
}